package com.selvn.encryptnote;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class NoteModel {
	
	// 取所有记录
	public static ArrayList<HashMap<String, String>> getNotes( Context context, String orderBy )
	{
		return getNotes(context, null, orderBy);
	}
	// 根据条件取记录
	public static ArrayList<HashMap<String, String>> getNotes( Context context, String selection, String orderBy )
	{
		Uri uri = Uri.parse("content://" + NotesContentProvider.AUTHORITY + "/note");
		ContentResolver cr = context.getContentResolver();
		String[] columnStrings = new String[]{ 
				Config.DBID,
				Config.DBTITLE,
				Config.DBBODY,
				Config.DBCREATEDATE,
				Config.DBMODIFY,
		};
		Cursor cursor = cr.query(uri, columnStrings, selection, null, orderBy);
		//Log.i(Config.LOGTAG, "count is " + Integer.toString( cursor.getCount() ));
		ArrayList<HashMap<String, String>> noteList = notelist_cursor_to_hashmap(cursor);
		cursor.close();
		return noteList;
	}
	public static HashMap<String, String> note_cursor_to_hashmap( Cursor cursor )
	{
		HashMap<String, String> itemHashMap = new HashMap<String, String>();
		itemHashMap.put(Config.DBID, cursor.getString( cursor.getColumnIndex(Config.DBID)) );
		itemHashMap.put(Config.DBTITLE, cursor.getString( cursor.getColumnIndex(Config.DBTITLE)) );
		itemHashMap.put(Config.DBBODY, cursor.getString( cursor.getColumnIndex(Config.DBBODY)) );
		String createString = Func.date_YMD( cursor.getString( cursor.getColumnIndex(Config.DBCREATEDATE)) );
		itemHashMap.put(Config.DBCREATEDATE, createString );
		createString = Func.date_YMD( cursor.getString( cursor.getColumnIndex(Config.DBMODIFY)) );
		itemHashMap.put(Config.DBMODIFY, createString );
		return itemHashMap;
	}
	public static ArrayList<HashMap<String, String>> notelist_cursor_to_hashmap( Cursor cursor ) {
		ArrayList<HashMap<String, String>> notesList = new ArrayList<HashMap<String,String>>();
		while (cursor.moveToNext()) {
			notesList.add(note_cursor_to_hashmap(cursor));
		}
		return notesList;
	}
	// 新建一条记录
	public static Uri insert( Context context, ContentValues values ) {
		Uri uri = Uri.parse("content://" + NotesContentProvider.AUTHORITY + "/note");
		Date nowDate = new Date();
		if( values.containsKey(Config.DBCREATEDATE))
		{
			String dateString = values.get(Config.DBCREATEDATE).toString(); 
			if( dateString.indexOf("-") >= 0 )
			{
				Date tmpDate = Func.date_parse(dateString);
				values.remove(Config.DBCREATEDATE);
				values.put(Config.DBCREATEDATE, Long.toString(tmpDate.getTime()));
			}
		}
		else
		{
			values.put(Config.DBCREATEDATE, Long.toString( nowDate.getTime() ));
		}
		if( values.containsKey(Config.DBMODIFY))
		{
			String dateString = values.get(Config.DBMODIFY).toString(); 
			if( dateString.indexOf("-") >= 0 )
			{
				Date tmpDate = Func.date_parse(dateString);
				values.remove(Config.DBMODIFY);
				values.put(Config.DBMODIFY, Long.toString((tmpDate.getTime()) ) );
			}
		}
		else
		{
			values.put(Config.DBMODIFY, Long.toString( nowDate.getTime() ));
		}
		ContentResolver contentResolver = context.getContentResolver();
		Uri returnUri = contentResolver.insert(uri, values);
		return returnUri;
	}
	public static int update( Context context, ContentValues values, Long noteId ) {
		Uri uri = Uri.parse("content://" + NotesContentProvider.AUTHORITY + "/note/" + noteId);
		Date nowDate = new Date();
		values.put(Config.DBMODIFY, Long.toString( nowDate.getTime() ));
		ContentResolver contentResolver = context.getContentResolver();
		int count = contentResolver.update(uri, values, null, null);
		return count;
	}
	public static int delete( Context context, Long noteId ) {
		Uri uri = Uri.parse("content://" + NotesContentProvider.AUTHORITY + "/note/" + noteId);
		ContentResolver contentResolver = context.getContentResolver();
		int count = contentResolver.delete(uri, null, null);
		return count;
	}
	// query a specific record
	public static HashMap<String, String> getNote( Context context, String noteId )
	{
		ContentResolver contentResolver = context.getContentResolver();
		Uri uri = Uri.parse("content://" + NotesContentProvider.AUTHORITY + "/note/" + noteId);
		Cursor cr = contentResolver.query(uri, null, null, null, null);
		HashMap<String, String> noteHashMap = null;
		if( cr.getCount() == 1 )
		{
			cr.moveToFirst();
			noteHashMap = note_cursor_to_hashmap( cr );
		}
		cr.close();
		return noteHashMap;
	}
	
	// backup and restore
	public static String GetListXml( Context context)
    {
        //Toast.makeText(getApplicationContext(), "Get List Xml", Toast.LENGTH_LONG).show();
        StringBuilder sb = new StringBuilder();
        sb.append("<lists>");
        ArrayList<HashMap<String, String>> noteList = NoteModel.getNotes(context, null);
        for (HashMap<String, String> hashMap : noteList)
		{
        	sb.append("<item id='"+ hashMap.get( Config.DBID) +"'>");
            sb.append("<title>"+ hashMap.get( Config.DBTITLE) +"</title>");
            sb.append("<body>"+ hashMap.get( Config.DBBODY) +"</body>");
            sb.append("<modifyDate>"+ hashMap.get( Config.DBMODIFY ) +"</modifyDate>");
            sb.append("<createDate>"+ hashMap.get( Config.DBCREATEDATE ) +"</createDate>");
            sb.append("<location1>"+ hashMap.get( Config.DBLOCATION1 ) +"</location1>");
            sb.append("<location2>"+ hashMap.get( Config.DBLOCATION2 ) +"</location2>");
            sb.append("</item>");
		}
        sb.append("</lists>");
        //Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_LONG).show();
        return sb.toString();
    }
}
